import asyncio
import time

from NestPageIterator import NextPageIterator
from downloader.download_img import download


async def main(url):
    # https://www.vcg.com/creative-image/chaye/
    # 列表页迭代器
    detail_page_list = NextPageIterator(url)
    # 遍历时即启动迭代器，不断得到下一页的列表页
    async for item in detail_page_list:
        start = time.time()
        tasks = []
        for detail_pages in item.__getattribute__('detail_page_list'):
            # print(detail_pages)
            tasks.append(download(detail_pages))

        done, padding = await asyncio.wait(tasks)
        print('下载成功数' + str(len(done)))
        print("下载一页耗时：", time.time() - start)
        spend = time.time() - start
        # spiderGUI.app.write_log_to_text('下载成功数' + str(len(done)))


def start(sy_url):
    if sy_url:
        asyncio.run(main(url=sy_url))


if __name__ == '__main__':
    start('https://www.vcg.com/creative-image/chaye/')
